#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <cstring>
#include <ctype.h>
using namespace std;
int dp[20000][20000];
int minwb(char* a, int m, int n)
{
	int i, j;
	for (int i = 0; i <= n; i++)
	{
		dp[i][0] = i;
	}
	for (j = 0; j <= m; j++)
	{
		dp[0][j] = j;
	}
	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
			if (a[i - 1] == 'B')
			{
				if (j >= 2)
				{
					dp[i][j] = dp[i - 1][j - 1];
				}
				else
				{
					dp[i][j] = dp[i - 1][j] + 1;
				}
			}
			else
			{
				int k = 0;
				while (k < j && a[i - k - 1] == 'B') {
					k++;
				}
				if (k < j)
				{
					dp[i][j] = dp[i - 1 - k][j - k - 1] + k + 1;
				}
				else
				{
					dp[i][j] = dp[i - 1][j - 1] + 1;
				}
			}
		}
	}
	return dp[n][m];
}
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n, m;
		cin >> n;
		cin >> m;
		char* a = (char*)malloc(n);
		cin >> a;
		cout << minwb(a, m, n)<<endl;
	}
}